﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraGrid.Views.Grid;
using System.Diagnostics;
using DTO;
using BUS;
using System.Data.SqlClient;
using DevExpress.XtraPrinting;

namespace GPBenhLanh
{
    public partial class frmTimKQ : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public frmTimKQ()
        {
            InitializeComponent();
            initControl(this);          
        }
        public frmTimKQ(int sel)
        {
            InitializeComponent();
            initControl(this);
            select = sel;
        }
        private int select;
        
        private static string strPathHinh = "";
        private bool firtLoad = true;//flag danh dau lau dau tien load len
        private int[] dataRes = new int[3];
  
        string date1, date2;

        private void initControl(Control ctrl)
        {

            // Bỏ qua điều kiểm trừ khi nó là TextBox.
            if (ctrl.Tag == "text")
            {
                ctrl.Text = "";
            }

            // Xử lý các điều kiểm một cách đệ quy.
            // Điều này cần thiết khi có một điều kiểm chứa nhiều 
            // điều kiểm khác (ví dụ, khi bạn sử dụng Panel, 
            // GroupBox, hoặc điều kiểm container nào khác).
            foreach (Control ctrlChild in ctrl.Controls)
            {
                initControl(ctrlChild);
            }
        }
       
        /// <summary>
        /// frmMain_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_Load(object sender, EventArgs e)
        {           
            //set ngay thang nam
            datTuNgay.DateTime = DateTime.Now.Date.AddDays(-365);
            datDenNgay.DateTime = DateTime.Now.Date; 
            firtLoad = false;

            date1 = "";
            date2 = "";
        }
                
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        DataTable dtKQ = new DataTable();
        private void btnTim_Click(object sender, EventArgs e)
        {   
            string strFilter = "1=1"; 

            DataRow[] filteredRows;

            //ho ten bn
            string [] HoTenBN = txtHoTenBN.Text.TrimStart(' ').TrimEnd(' ').Split('%');
            for (int i = 0; i < HoTenBN.Length; i++)
            {
                if (HoTenBN[i].Equals("") == false)
                {
                    strFilter += string.Format(" AND {0} LIKE '%{1}%'", "HoTen", HoTenBN[i]);
                }
            }

            //ho ten bs
            string[] HoTenBS = txtHoTenBS.Text.TrimStart(' ').TrimEnd(' ').Split('%');
            for (int i = 0; i < HoTenBS.Length; i++)
            {
                if (HoTenBS[i].Equals("") == false)
                {
                    strFilter += string.Format(" AND {0} LIKE '%{1}%'", "bsith", HoTenBS[i]);
                }
            }

            //chan doan
            string[] ChuanDoan = txtChanDoan.Text.TrimStart(' ').TrimEnd(' ').Split('%');
            for (int i = 0; i < ChuanDoan.Length; i++)
            {
                if (ChuanDoan[i].Equals("") == false)
                {
                    strFilter += string.Format(" AND {0} LIKE '%{1}%'", "ChuanDoan", ChuanDoan[i]);
                }
            }

            //dai the
            string[] DaiThe = txtDaiThe.Text.TrimStart(' ').TrimEnd(' ').Split('%');
            for (int i = 0; i < DaiThe.Length; i++)
            {
                if (DaiThe[i].Equals("") == false)
                {
                    strFilter += string.Format(" AND {0} LIKE '%{1}%'", "KQDaiThe", DaiThe[i]);
                }
            }

            //vi the
            string[] ViThe = txtViThe.Text.TrimStart(' ').TrimEnd(' ').Split('%');
            for (int i = 0; i < ViThe.Length; i++)
            {
                if (ViThe[i].Equals("") == false)
                {
                    strFilter += string.Format(" AND {0} LIKE '%{1}%'", "KQViThe", ViThe[i]);
                }
            }            

            //ket luan
            string []KetLuan = txtKetLuan.Text.TrimStart(' ').TrimEnd(' ').Split('%');           
            for (int i = 0; i < KetLuan.Length; i++)
            {
                if (KetLuan[i].Equals("") == false)
                {
                    strFilter += string.Format(" AND {0} LIKE '%{1}%'", "KetLuan", KetLuan[i]);
                }
            }

            DataTable dtRs = new DataTable();
            dtRs = dtKQ.Clone();
            DataRow drRs;
            filteredRows = dtKQ.Select(strFilter);
            for (int i = 0; i < filteredRows.Length; i++)
            {
                drRs = dtRs.NewRow(); 
                for (int j = 0; j < filteredRows[i].ItemArray.Length; j++)
                {
                    drRs[j] = filteredRows[i].ItemArray[j];
                }
                dtRs.Rows.Add(drRs);
            }
            dtRs.DefaultView.Sort = "CDate desc";
            gridMain.DataSource = dtRs;
            gridMain.Refresh();
                
            initControl(this);
        }
                
        private void gridMain_Click(object sender, EventArgs e)
        {
            try
            {
                //DataRow dr;
                //dr = griddsBenhNhan.GetDataRow(griddsBenhNhan.FocusedRowHandle);
                //if (dr != null)
                //{
                //    set value to information panel
                //    txtHoTen.Text = dr["HoTen"].ToString();
                //    txtTuoi.Text = dr["nam"].ToString();
                //    txtGioi.Text = dr["gioi"].ToString();
                //    txtSoHS.Text = dr["SoHS"].ToString();
                //    txtSoBN.Text = dr["SoBN"].ToString();
                //    txtdiachi.Text = dr["DiaChi"].ToString();
                //    txtChuanDoan.Text = dr["ChuanDoan"].ToString();
                //    txtSoTieuBan.Text = dr["SoTieuBan"].ToString();
                //}                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Thông Báo");
            }
        }

        private void gridMain_KeyUp(object sender, KeyEventArgs e)
        {
            //gridMain_Click( sender,  e);
        }

        private void ReLoadForm(object sender)
        {
            //EventArgs e = new EventArgs ();
            //if (lblReload.Text == "true")
            //{
            //    btnTim_Click(sender, e);
            //}
        }

        public static void DanhSoThuTu(RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle < 0)
                    e.Info.DisplayText = "";
                else
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
            else
            {
                e.Info.DisplayText = "STT";
            }
            e.Info.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
        }
        private void griddsBenhNhan_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            DanhSoThuTu(e);
        }

        private void labelControl1_Click(object sender, EventArgs e)
        {

        }

        private void datTuNgay_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void txtHoTenBN_Validating(object sender, CancelEventArgs e)
        {

        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            var path = System.IO.Path.Combine(
                Environment.GetFolderPath(
                Environment.SpecialFolder.LocalApplicationData)
                , string.Format("{0}_{1}.xls", "Report", DateTime.Now.ToString("ddMMyyyy_HHmmss"))
                );
            XlsExportOptions xlOptions = new DevExpress.XtraPrinting.XlsExportOptions(DevExpress.XtraPrinting.TextExportMode.Text);
            ((DevExpress.XtraGrid.Views.Grid.GridView)(this.gridMain.DefaultView)).ExportToXls(path, xlOptions);
            System.Diagnostics.Process.Start(path);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            string STuNgay = DateTime.Parse(datTuNgay.EditValue.ToString())
                                  .ToString("yyyy-MM-dd");
            string SDenNgay = DateTime.Parse(datDenNgay.EditValue.ToString())
                                    .ToString("yyyy-MM-dd");

            dtKQ = ThongKeBUS.TimKetQua(STuNgay, SDenNgay);
            if (dtKQ.Rows.Count > 0)
            {
                pnlFiler.Enabled = true;
            }
            else
            {
                pnlFiler.Enabled = false;
            }
        }
    }
}